<?php

class user_cart_l_cart extends a_base_cart_l_cart {

    protected $cmc;

    public function __construct(user_m_user $user) {
        parent::__construct();
        $u = $user;
        if ($u === NULL)
            show_error("A user must be logged in for user cart instance");
        $site = service_l_core::get_current_site();
        $this->cmc = new cart_m_cart();
        $this->cmc->user_id = $u->id;
        $this->cmc->site_id = $site->id;
        if (!$this->cmc->get()->exists()) {
            $this->cmc->save();
        }
    }

    public function is_in_cart($product_id) {        
        $obj = $this->cmc->item_m_cart->get_by_product_id($product_id);
        return $obj->exists() ? $obj : NULL;
    }

    public function add_item($product_id, $quantity=1) {
        if (is_numeric($quantity) && $quantity > 0) {
            $p = new product_m_product();
            $p->get_by_id($product_id);
            
            if ($p->exists()) {

                $cart_item = NULL;
                if (($cart_item = $this->is_in_cart($product_id)) !== NULL) {
                    $cart_item->qty+=$quantity;
                    $cart_item->save();
                } else {
                    $cart_item = new item_m_cart();
                    $cart_item->product_id = $product_id;
                    $cart_item->qty = $quantity;
                    $cart_item->cart_id =$this->cmc->id;
                    $cart_item->save();
                }
            } else {
                throw new product_not_found_exception_l_cart($product_id);
            }
        } else {
            throw new invalid_qty_exception_l_cart($product_id, $quantity);
        }
    }

    public function update_item($product_id, $new_qty) {
        if (is_numeric($new_qty) && $new_qty > 0) {
            $p = new product_m_product();            
            if ($p->get_by_id($product_id)->exists()) {
                $cart_item = NULL;
                if (($cart_item = $this->is_in_cart($product_id)) !== NULL) {
                    $cart_item->qty =$new_qty;
                    return $cart_item->save();
                } else {
                    throw new not_in_cart_exception_l_cart($product_id);
                }
            } else {
                $cart_item = NULL;
                if (($cart_item = $this->is_in_cart($product_id))) {
                     $cart_item->delete();
                }
                throw new product_not_found_exception_l_cart($product_id);
            }
        } else {
            throw new invalid_qty_exception_l_cart($product_id, $new_qty);
        }
    }

    public function remove_item($product_id) {
        $cart_item = $this->is_in_cart($product_id);
        if ($cart_item) {
            return $cart_item->delete();
        } else {
            throw new not_in_cart_exception_l_cart($product_id);
        }
    }

    public function remove_all() {
        $item = new item_m_cart();
        $objs = $item->where('cart_id',$this->cmc->id)->get();
        if($item->count()){
            foreach($objs as $item){
                $item->delete();
            }
        }
    }

    public function is_empty() {
        $item = new item_m_cart();
        $objs = $item->where('cart_id',$this->cmc->id)->get();
        return $item->count()>0 ? FALSE : TRUE;
    }

    public function total_items() {
        $item = new item_m_cart();
        return $item->where('cart_id',$this->cmc->id)->get()->result_count();
   }

    public function get_all_items() {
        $item = new item_m_cart();
        $objs = $item->where('cart_id',$this->cmc->id)->get();
        return $objs;
    }

    
}

?>
